package openapi.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;

public class ResUtils {

	/**
	 * 
	 * @param location
	 * @return
	 */
	public static String respath(String location) {
		if (null == location) {
			throw new RuntimeException("[location] is null");
		}
		URL url = ResUtils.class.getClassLoader().getResource(location);
		return url.getPath();
	}

	/**
	 * 
	 * @param location
	 * @return
	 */
	public static String readFileContent(String location) {
		StringBuilder sbb = new StringBuilder();

		String filepath = respath(location);
		BufferedReader br = null;
		try {
			try {
				br = new BufferedReader(new FileReader(new File(filepath)));
				String line = null;
				while ((line = br.readLine()) != null) {
					sbb.append(line);
				}
			} finally {
				if (null != br) {
					br.close();
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sbb.toString();
	}
}
